﻿using AutoMapper;
using PowerHub.MES.DTO.BasicData.DTO;
using PowerHub.MES.DTO.BasicData.QueryDto;
using PowerHub.MES.EFBase;
using PowerHub.MES.Entities.BasicData;
using PowerHub.MES.Entities;
using PowerHub.MES.IRepository.IBasic;
using PowerHub.MES.IServices.IBasic;
using PowerHub.MES.Jwt;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PowerHub.MES.IServices.IShopfloor;
using PowerHub.MES.Entities.Shopfloor;
using PowerHub.MES.DTO.Shopfloor.QueryDTO;
using PowerHub.MES.DTO.Shopfloor.DTO;
using PowerHub.MES.IRepository.IShopfloor;
using PowerHub.MES.Common.NumberingCenter;

namespace PowerHub.MES.Services.Shopfloor
{
    /// <summary>
    /// 车间接口实现层
    /// </summary>
    public class Shopfloor_WorkshopinfoServices : BaseServices<Shopfloor_Workshopinfo>, IShopfloor_WorkshopinfoServices
    {
        private IShopfloor_WorkshopinfoRepository _iShopfloor_WorkshopinfoRepository;
        public UserServices _userServices;
        private IMapper _mapper;
        private static Random random = new Random();

        public Shopfloor_WorkshopinfoServices(PowerHubMesContext powerHubMesContext, IShopfloor_WorkshopinfoRepository iShopfloor_WorkshopinfoRepository, UserServices userServices, IMapper mapper) : base(powerHubMesContext)
        {
            _iShopfloor_WorkshopinfoRepository = iShopfloor_WorkshopinfoRepository;
            _userServices = userServices;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取车间列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationPageResult<List<Shopfloor_Workshopinfo>>> GetShopfloor_WorkshopinfoPageListAsync(QueryShopfloor_WorkshopinfoDTO query)
        {
            var ss = _userServices.UserId;
            var UserName = _userServices.UserName;
            var Role = _userServices.Role;
            return await _iShopfloor_WorkshopinfoRepository.GetShopfloor_WorkshopinfoPageListAsync(query);
        }

        /// <summary>
        /// 添加车间
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> CreateShopfloor_WorkshopinfoAsync(Shopfloor_WorkshopinfoDTO query)
        {
            Shopfloor_Workshopinfo Workshopinfo = await GetEntityAsync(d => d.WorkshopName.Equals(query.WorkshopName) && d.IsDel==0);
            if (Workshopinfo!=null)
            {
                throw new Exception("你输入的车间名称已存在！");
            }
            //实体转换
            var Shopfloor_Workshopinfo = _mapper.Map<Shopfloor_Workshopinfo>(query);
            Shopfloor_Workshopinfo.Id = Guid.NewGuid().ToString();
            Shopfloor_Workshopinfo.WorkshopNo = string.IsNullOrEmpty(query.WorkshopNo) || query.WorkshopNo.Contains(" ") ? GenerateBasicDataNO.GenerateCode(query.WorkshopName) : query.WorkshopNo;
            Shopfloor_Workshopinfo.Creator = _userServices.UserName;

            return await _iShopfloor_WorkshopinfoRepository.CreateShopfloor_WorkshopinfoAsync(Shopfloor_Workshopinfo);
        }


        /// <summary>
        /// 生成编号
        /// </summary>
        /// <param name="customCode"></param>
        /// <param name="sequenceLength"></param>
        /// <returns></returns>
        public string GetWorkshopinfoNO(string customCode = "56", int sequenceLength = 5)
        {
            Shopfloor_Workshopinfo shopfloor_Workshopinfo = GetEntityListAsync(u => u.IsDel == 0).Result.OrderByDescending(u => u.WorkshopNo).FirstOrDefault();

            int sequenceNumber = 0;
            if (shopfloor_Workshopinfo != null)
            {
                string JobNumber = shopfloor_Workshopinfo.WorkshopNo.Substring(shopfloor_Workshopinfo.WorkshopNo.Length - 5);
                sequenceNumber = Convert.ToInt32(JobNumber);
            }
          
            // 获取当前年份的简写（只取后两位）  
            string yearShort = DateTime.Now.Year.ToString().Substring(2, 2);
            sequenceNumber++;
            string sequence = sequenceNumber.ToString().PadLeft(sequenceLength, '0');

            return yearShort + customCode + sequence;
        }

        /// <summary>
        /// 批量删除车间
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> DeletionShopfloor_WorkshopinfoAsync(List<string>? ids)
        {
            if (ids.Count < 1)
            {
                throw new Exception("请先选择数据！");
            }
            return await _iShopfloor_WorkshopinfoRepository.DeletionShopfloor_WorkshopinfoAsync(ids);
        }


        /// <summary>
        /// 更新车间
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> UpdateShopfloor_WorkshopinfoAsync(Shopfloor_WorkshopinfoDTO query)
        {
            if (query == null)
            {
                throw new Exception("请先选择数据！");
            }

            //根据id获取车间信息
            Shopfloor_Workshopinfo Shopfloor_Workshopinfo = await GetEntityAsync(d => d.Id.Equals(query.Id));
            if (Shopfloor_Workshopinfo == null)
            {
                throw new Exception("更新车间信息发生了异常,请刷新页面后试试");
            }
            Shopfloor_Workshopinfo.WorkshopName = query.WorkshopName;
            Shopfloor_Workshopinfo.Area = query.Area;
            Shopfloor_Workshopinfo.UserId = query.UserId;
            Shopfloor_Workshopinfo.FoundationId = query.FoundationId;
            Shopfloor_Workshopinfo.Area = query.Area;
            Shopfloor_Workshopinfo.Updator = _userServices.UserName;
 

            return await _iShopfloor_WorkshopinfoRepository.UpdateShopfloor_WorkshopinfoAsync(Shopfloor_Workshopinfo);

        }
    }
}
